/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2022 All Rights Reserved.
 */
package com.example.spider.proxy.provider.deprecated.dieniao;

import cn.hutool.core.util.ReUtil;
import com.example.spider.proxy.core.ProxyInfo;
import com.example.spider.proxy.provider.AbstractProxyResultHandler;
import com.example.spider.proxy.provider.IpProxySpiderTask;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.task.SpiderTaskQueue;

import java.time.Duration;
import java.util.List;

/**
 * @author lym
 * @deprecated 2023.4 已经不提供免费了
 * 蝶鸟
 * https://www.dieniao.com/FreeProxy.html 【免费只有一页】
 * 每五分钟一次
 */
//@Component
public class DieNiaoResultHandler extends AbstractProxyResultHandler implements SpiderStarter {

    public DieNiaoResultHandler() {
        super("dieNiao");
    }

    protected Duration defaultLimitRateDuration() {
        return Duration.ofSeconds(0);
    }

    @Override
    public void startSpiders() {
        IpProxySpiderTask task = createTask("https://www.dieniao.com/FreeProxy.html");
        SpiderTaskQueue.putTask(task);
    }

    /**
     * 每 5 分钟触发一次
     */
    @Override
    public String executeCron() {
        return "0 0/5 * * * ?";
    }

    /**
     * <li class='f-list f-top col-lg-12 col-md-12 col-sm-12 col-xs-12'>
     * <span class='f-address'>IP地址</span>
     * <span class='f-port'>端口</span>
     * <span class='f-port'>类型</span>
     * <span class='f-locat'>归属地</span>
     * <span class='f-port'>运营商</span>
     * <span class='f-speed'>响应速度</span>
     * <span class='f-latime'>最后验证时间</span>
     * <span class='f-latime'>存活时间</span>
     * </li>
     *
     * <li class='f-list col-lg-12 col-md-12 col-sm-12 col-xs-12'>
     * <span class='f-address'>120.220.220.95</span>
     * <span class='f-port'>8085</span>
     * <span class='f-port'>普匿</span>
     * <span class='f-locat'>山东省</span>
     * <span class='f-port'>移动</span>
     * <span class='f-speed'>0.19 秒</span>
     * <span class='f-latime'>7小时23分前
     * </span>
     * <span class='f-latime'>
     * 167天
     * </span>
     * </li>
     */
    @Override
    public void doHandle(IpProxySpiderTask task, String html) {

        String regex = "<span class='f-address'>(.*?)</span>\n"
                + "<span class='f-port'>(.*?)</span>\n"
                // 匿名：普匿 高匿 未知
                + "<span class='f-port'>(.*?)</span>\n"
                + "<span class='f-locat'>(.*?)</span>\n"
                // 运营商
                + "<span class='f-port'>(.*?)</span>\n"
                + "<span class='f-speed'>(.*?) 秒</span>";

        List<String> ipList = ReUtil.findAll(regex, html, 1);
        List<String> portList = ReUtil.findAll(regex, html, 2);
        List<String> anonymityList = ReUtil.findAll(regex, html, 3);
        List<String> locationList = ReUtil.findAll(regex, html, 4);
        List<String> operatorList = ReUtil.findAll(regex, html, 5);
        List<String> responseTimeList = ReUtil.findAll(regex, html, 6);

        // 第一行是表头，跳过
        for (int i = 1; i < ipList.size(); i++) {
            ProxyInfo proxyInfo = ProxyInfo.builder()
                    .source(taskName)
                    .ip(ipList.get(i))
                    .port(Integer.parseInt(portList.get(i)))
                    .anonymity(anonymityList.get(i))
                    .location(locationList.get(i))
                    .operator(operatorList.get(i).replace("&nbsp;", "").trim())
                    .responseTime(ProxyInfo.parseSecondToMill(responseTimeList.get(i)))
                    .build();
            // 保存到文件
            addProxyToSourceRepository(proxyInfo);
        }

    }

}